
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../utils/Settings.dart';
import 'LFormTreeLogic.dart';
import 'LFormTreeState.dart';

class TreeSingleSelect extends StatefulWidget{
  const TreeSingleSelect({super.key, required this.state, required this.tag});
  final LFormTreeState state;
  final String tag;

  @override
  State<TreeSingleSelect> createState() =>TreeSingleSelectState();

}

class TreeSingleSelectState extends State<TreeSingleSelect> with AutomaticKeepAliveClientMixin{


  List<Widget> tree = [];
  Map<String,double> nodeLeavel = {};

  @override
  void initState() {
    super.initState();
  }

  List<Widget> createTree(options){
    for(var item in options){
      if(nodeLeavel.isEmpty){
        nodeLeavel[item["key"]]=1;
      }
      tree.add(
          Padding(
            padding: EdgeInsets.only(left:20*nodeLeavel[item["key"]]!,top:15),
            child: (
                InkWell(
                  child: Row(
                    children: [
                      item["children"]!=null?Image.asset("images/tree_node_p.png",width: 16):Image.asset("images/tree_node_c.png",width: 16),
                      const SizedBox(width: 2,),
                      // item["children"]!=null?const Icon(Icons.folder_open):const Icon(Icons.insert_drive_file_outlined),
                      item["title"]==widget.state.defaultText?Text(item["title"],style: const TextStyle(color: Settings.primaryColor),):Text(item["title"])
                    ],
                  ),
                  onTap: ()async{
                    Get.find<LFormTreeLogic>(tag: widget.tag).onSelected(item);
                    Get.back();
                  },
                )
            ),
          )
      );
      if(item["children"]!=null){
        for(var c in item["children"]){
          nodeLeavel[c["key"]] = nodeLeavel[item["key"]]! +1;
        }
        createTree(item["children"]);
      }
    }
    return tree;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;

  ///开始自定义业务





  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Column(
        children: createTree(widget.state.dictOptions),
      ),
    );
  }

}